{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(\"../\")\n",
    "from initial_dataSet import DataSet\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "from sklearn.cluster import KMeans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataSet: MathEC\n"
     ]
    }
   ],
   "source": [
    "basedir = '../'\n",
    "models=['CICDM','CICDM-A','CDGK','NCD','DINA']\n",
    "\n",
    "model=models[4]\n",
    "dataSet_list = ('ASSIST_0910', 'ASSIST_2017','JUNYI', 'MathEC')\n",
    "save_list = ('a0910/', 'a2017/', 'junyi/', 'math_ec/')\n",
    "\n",
    "dataSet_idx=3\n",
    "data_set_name = dataSet_list[dataSet_idx]\n",
    "dataSet = DataSet(basedir, data_set_name,build=True)\n",
    "\n",
    "if model=='CICDM':\n",
    "    read_dir=dataSet.save_result_dir\n",
    "elif model=='CICDM-A':\n",
    "    read_dir='./output/'+save_list[dataSet_idx]\n",
    "elif model=='CDGK':\n",
    "    read_dir='E:/PY_Project/知识点交互CDM/temp_Baseline/CDGK/output/'+save_list[dataSet_idx]\n",
    "elif model=='NCD':\n",
    "    read_dir='E:/PY_Project/知识点交互CDM/Baseline/NCD/output/'+save_list[dataSet_idx]\n",
    "elif model=='DINA':\n",
    "    read_dir='E:/PY_Project/知识点交互CDM/Baseline/DINA/output/'+save_list[dataSet_idx]\n",
    "else:\n",
    "    assert False,'模型名称错误'\n",
    "\n",
    "cogn_state=np.loadtxt(read_dir+'cognitive_state.csv',delimiter=',')\n",
    "record=dataSet.record.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [],
   "source": [
    "cluster_k=4\n",
    "\n",
    "cluster=KMeans(n_clusters=cluster_k)\n",
    "cluster.fit(cogn_state)\n",
    "labels=cluster.labels_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [],
   "source": [
    "record['cluster']=labels[record['user_id']-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [],
   "source": [
    "item_mean_score=np.ones((cluster_k,dataSet.exercise_num))*-1e10\n",
    "for cluster_num in range(cluster_k):\n",
    "    clust_record=record.set_index('cluster').loc[cluster_num,:]\n",
    "    mean_score=clust_record.groupby('item_id').mean()['score']\n",
    "    item_mean_score[cluster_num,mean_score.index-1]=mean_score.values\n",
    "item_mean_score[item_mean_score<-1e5]=np.nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DINA MathEC\n",
      "熟练度区分度: 0.09348604608570495\n"
     ]
    }
   ],
   "source": [
    "centers=cluster.cluster_centers_\n",
    "cent_abs_list=[]\n",
    "for i in range(cluster_k):\n",
    "    for j in range(i+1,cluster_k):\n",
    "        diff=centers[i]-centers[j]\n",
    "        diff=diff[~np.isnan(diff)]\n",
    "        cent_abs_list.append(np.abs(diff).mean())\n",
    "\n",
    "print(model,data_set_name)\n",
    "print('熟练度区分度:',np.mean(cent_abs_list)/record['score'].mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DINA MathEC\n",
      "得分区分度: 0.35268321720054474\n"
     ]
    }
   ],
   "source": [
    "abs_list=[]\n",
    "for i in range(cluster_k):\n",
    "    for j in range(i+1,cluster_k):\n",
    "        diff=item_mean_score[i]-item_mean_score[j]\n",
    "        diff=diff[~np.isnan(diff)]\n",
    "        abs_list.append(np.abs(diff).mean())\n",
    "\n",
    "print(model,data_set_name)\n",
    "print('得分区分度:',np.mean(abs_list)/record['score'].mean())"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "527a93331b4b1a8345148922acc34427fb7591433d63b66d32040b6fbbc6d593"
  },
  "kernelspec": {
   "display_name": "Python 3.8.12 ('pytorch')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
